using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Validation;
using Microsoft.Practices.EnterpriseLibrary.Validation.Validators;

namespace CustomerSample
{
	public class LessThanHundredAndFiftyYearsValidatorAttribute : ValueValidatorAttribute
	{
		protected override Validator DoCreateValidator(Type targetType)
		{
			return new LessThanHundredAndFiftyYearsValidator(null, null, false);
		}
	}

	public class LessThanHundredAndFiftyYearsValidator : ValueValidator<DateTime>
	{
		public LessThanHundredAndFiftyYearsValidator(string messageTemplate, string tag, bool negated)
			: base(messageTemplate, tag, negated)
		{
		}

		protected override void DoValidate(
			DateTime objectToValidate,
			object currentTarget,
			string key,
			ValidationResults validationResults)
		{
			if ((DateTime.Now.Year - objectToValidate.Year) > 150)
			{
				validationResults.AddResult(new ValidationResult(
					DefaultMessageTemplate, currentTarget, key, null, this));
			}
		}

		protected override string DefaultNonNegatedMessageTemplate
		{
			get { return "Date can't be more than 150 years ago"; }
		}

		protected override string DefaultNegatedMessageTemplate
		{
			get { return "Date must be more than 150 years ago"; }
		}
	}
}
